home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14918 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: nyx10.cs.du.edu!not-for-mail
  2. From: cwyles@nyx10.cs.du.edu (Carl Wyles)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Sorting a Binary File
  5. Date: 15 Apr 1996 19:19:27 -0600
  6. Organization: University of Denver, Math/CS Dept.
  7. Message-ID: <4kusiv$pgt@nyx10.cs.du.edu>
  8. References: <4krpuf$jfp@news1.sympatico.ca>
  9. NNTP-Posting-Host: nyx10.nyx.net
  10. X-Newsreader: NN version 6.5.0 #3 (NOV)
  11.  
  12. Gisele Swinson <gisele.swinson@sympatico.ca> writes:
  13.  
  14. >I have declared a structure as follows:
  15.  
  16. >#define max 5
  17.  
  18. >struct customer
  19. >{
  20. >     int seatno;
  21. >     char lastname[15];
  22. >} Customer[max];
  23.  
  24. >I have also initialized the structure as the seat numbers 1-5 and the last 
  25. >name as blank
  26.  
  27. >My program summarized is I enter a seat number then enter the name, when I 
  28. >cam finished, save the data to a binary file. I want to sort the 
  29. >structures in last name order.  I tried using bubble sort but I'm not 
  30. >being very successful.
  31.  
  32. >This is what I tried:
  33.  
  34. >for(pass=1; pass<max; pass++)
  35. >   for(i = 0; i<max-1; i++)
  36.  
  37. >      if(Customer[i].lastname > Customer[i+1].lastname)
  38. >      {
  39. >          temp = Customer[i];
  40. >          Customer[i] = Customer[i+1];
  41. >          Customer[i+1] = temp;
  42. >      }
  43. Above code is undefined on most C compilers because of the way strings 
  44. are defined. I am not sure what the C++ compilers would do.
  45. A string in C is nothing more than and array of characters. This is what 
  46. causes most problems with string comparison.
  47.  
  48. >      printf("\nSorted customer list");
  49. >      printf("\nSeat number   Customer Name"):
  50. >      
  51. >      for(x=0; x<max; x++)
  52. >           printf("\n%2d\t %s", Customer[x].seatno, Customer[x].lastname);
  53.  
  54. Well, I would suggest the following for starters...
  55.  
  56. /* sort - indexed bubble */
  57. for( x = 0; x < filled; x++ ) {
  58.     idx[ x ] = x; /* set up index to values */
  59. }
  60. /* filled = number of elements with data */
  61. for( pass = 0; pass < filled - 1; pass++ ) { 
  62.     for( x = pass; x < filled; x++ ) {
  63.         /* use strcmp to compare strings */
  64.         if( strcmp( Customer[ idx[ x + 1 ] ].lastname, 
  65.             Customer[ idx[ x ] ].lastname ) < 0 )
  66.             /* only move the index values */
  67.             temp = idx[ x ];
  68.             idx[ x ] = idx[ x + 1 ];
  69.             idx[ x + 1 ] = temp;
  70.         }
  71.     }
  72. }
  73.  
  74. printf( "Sorted list" );
  75. for( x = 0; x < filled; x++ ) {
  76.     printf("\n%2d\t %s", Customer[ idx[ x ] ].seatno, 
  77.                Customer[ idx[ x ] ].lastname );
  78. }
  79.  
  80. I said this was for starters :)
  81.  
  82. If you want to make it faster use some other type of sort routine. QSORT 
  83. is usually in most C libraries. You want to use an index because it is 
  84. faster than moving the whole data item. 
  85.  
  86. -- 
  87. >>>>> All flames sent to Demon Dragon familiar for appropriate response <<<<<
  88. cwyles@nyx10.cs.du.edu | My words are the amalgamation of my experiences only
  89. Good advice is ignored. Ok advice is used. Bad advice is flamed forever.
  90.